import { showdate, weekonly, timeonly, time } from '../../utils/util'
Page({
  data: {
    // 门店
    option1: [],
    option2: [],
    option3: [],
    option4: [],
    //日期
    weekList: [],
    // 游戏列表
    gameList: [],
    active: 0,
    params: {
      game_date: timeonly(0),
    },
  },

  onLoad({ s_id }) {
    if (s_id) {
      this.setData({
        'params.s_id': s_id - 0,
      })
    }
    this._getSelect1()
    this._getSelect2()
    this._getSelect3()
    this._getSelect4()
    this._strTime()
    this._getGameList()
  },
  //  筛选
  _handleChane({
    detail,
    target: {
      dataset: { index },
    },
  }) {
    let id = detail == '' ? '' : detail
    // 门店切换
    if (index == 0) {
      this.setData({
        'params.s_id': id,
      })
    }
    // 游戏类别
    else if (index == 1) {
      this.setData({
        'params.gsc_id': id,
      })
    }
    // 老师id
    else if (index == 2) {
      this.setData({
        'params.t_id': id,
      })
    }
    // 年龄
    else if (index == 3) {
      this.setData({
        'params.age': id,
      })
    }
    this._getGameList()
  },
  // 获取游戏列表
  async _getGameList() {
    const { params } = this.data
    let { code, data } = await wx.requestAsync({
      url: '/game/spell',
      data: {
        page: 1,
        ...params,
      },
    })
    if (code == 0) {
      let gameList = data.map((item) => {
        item.begin_at = time(item.begin_at)
        item.end_at = time(item.end_at)
        return item
      })
      this.setData({
        gameList,
      })
    }
  },
  // 获取时间
  _strTime() {
    let week = ['一', '二', '三', '四', '五', '六', '七']
    let weekList = []
    week.map((item, index) => {
      weekList.push({ date: showdate(index), weekItem: weekonly(index) })
    })
    this.setData({
      weekList,
    })
  },
  // 获取门店筛选信息
  async _getSelect1() {
    let { code, data } = await wx.requestAsync({
      url: '/shop/select',
    })
    if (code == 0) {
      let option1 = data.map((item) => {
        item.text = item.shop_name
        item.value = item.s_id
        return item
      })
      this.setData({
        option1: [{ text: '全部', value: '' }, ...option1],
      })
    }
  },
  // 获取游戏类别信息
  async _getSelect2() {
    let { code, data } = await wx.requestAsync({
      url: '/game/cat',
    })
    if (code == 0) {
      let option2 = data.map((item) => {
        item.text = item.name
        item.value = item.gsc_id
        return item
      })
      this.setData({
        option2: [{ text: '全部', value: '' }, ...option2],
      })
    }
  },
  // 获取老师类别信息
  async _getSelect3() {
    let { code, data } = await wx.requestAsync({
      url: '/teacher',
    })
    if (code == 0) {
      let option3 = data.map((item) => {
        item.text = item.name
        item.value = item.t_id
        return item
      })
      this.setData({
        option3: [{ text: '全部', value: '' }, ...option3],
      })
    }
  },
  // 获取游戏年龄信息
  async _getSelect4() {
    let { code, data } = await wx.requestAsync({
      url: '/game/age',
    })
    if (code == 0) {
      let option4 = []
      data.map((item) => {
        option4.push({ text: item + '岁', value: item })
      })
      this.setData({
        option4: [{ text: '全部', value: '' }, ...option4],
      })
    }
  },
  // 选择时间
  _handle({ currentTarget }) {
    let { id } = currentTarget.dataset
    this.setData({
      active: id,
      'params.game_date': timeonly(id),
    })
    this._getGameList()
  },
  _handleTo({ currentTarget }) {
    let { id } = currentTarget.dataset
    wx.navigateTo({
      url: '/pages/game/game?gss_id=' + id,
    })
  },
  // 确认订单的
  _toGruoporders({ currentTarget }) {
    let { id } = currentTarget.dataset
    wx.navigateTo({
      url: '/pages/grouporders/grouporders?gss_id=' + id,
    })
  },
})
